 /**
 * Description: 门店信息
 * @author:Hunter
 * @since:1.0.0
 *
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2018-03-27   Hunter       1.0.0     Initial
 */
package com.metro.demo.biz.service.impl;

import com.metro.core.dao.IMapper;
import com.metro.core.excel.ExcelUtil;
import com.metro.core.service.BaseServiceSupport;
import com.metro.core.service.ServiceException;
import com.metro.core.vo.JsonResult;
import com.metro.core.vo.ResultCode;
import com.metro.core.vo.inf.IResult;
import com.metro.demo.biz.beans.vo.BizVersionHistory;
import com.metro.demo.biz.mapper.IBizVersionHistoryMapper;
import com.metro.demo.common.beans.constant.CommonConstant;
import com.metro.demo.common.beans.constant.PropertiesEnum;
import com.metro.demo.sys.beans.vo.SysPermission;
import com.metro.demo.web.controller.imp.ImportResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.metro.demo.biz.mapper.IBiz4sinfoMapper;
import com.metro.demo.biz.service.inf.IBiz4sinfoService;
import com.metro.demo.biz.beans.vo.Biz4sinfo;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.*;

 /**
 * 门店信息<br>
 *
 * @author:Hunter
 * @since:1.0.0
 * @version [1.0.0, 2018-03-27]
 */
@Service("biz4sinfoService")
public class Biz4sinfoService extends BaseServiceSupport<Biz4sinfo> implements IBiz4sinfoService{
    //门店信息Mapper
    @Autowired
    private IBiz4sinfoMapper biz4sinfoMapper;
    //版本历史Mapper
     @Autowired
    private IBizVersionHistoryMapper bizVersionHistoryMapper;

    @Override
    public IMapper getMapper() {
        return biz4sinfoMapper;
    }

    @Override
    public IResult save(Biz4sinfo entity) {
        try {
            biz4sinfoMapper.save(entity);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("新增门店信息失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult update(Biz4sinfo entity) {
        //前台传过来的数据
        String version = entity.getSysVersion();
        Date upgradeTime = entity.getUpgradeTime();


        //判断前台传过来的版本号和升级时间是否为空
        if(StringUtils.isNotBlank(version) && upgradeTime != null){
            //根据id查询库中的信息
            Biz4sinfo queryEntity = getById(entity.getId());
            String version1 = queryEntity.getSysVersion();
            Date upgradeTime1 = queryEntity.getUpgradeTime();
            //往history表中插数据
            BizVersionHistory historyEntity = new BizVersionHistory();
            historyEntity.setCode(entity.getCord());
            historyEntity.setName(entity.getName());
            historyEntity.setSysVersion(version);
            historyEntity.setUpgradeTime(upgradeTime);

            if(StringUtils.isNotBlank(version1) && upgradeTime1 != null){
                //判断版本是否修改过
                boolean b1 = version.equals(version1);
                //判断更新时间是否修改过
                boolean b2 = upgradeTime.getTime() == upgradeTime1.getTime();
                //如果版本和更新时间都修改过 则往history表中插一条历史纪录
                if(!b1 && !b2) {
                    bizVersionHistoryMapper.save(historyEntity);
                }else if (!b1 && b2){//如果只有版本修改过 则update history表中版本号
                    bizVersionHistoryMapper.updateVersion(historyEntity);
                }else if (b1 && !b2){//如果只修改更新时间 则update history表中更新时间
                    bizVersionHistoryMapper.updateUpgradeTime(historyEntity);
                }
            } else{
                bizVersionHistoryMapper.save(historyEntity);
            }
        }

        try {
            biz4sinfoMapper.update(entity);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("修改门店信息失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult delete(Long id) {
        try {
            biz4sinfoMapper.delete(id);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("删除失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult deleteByIds(String ids) {
        try {
            biz4sinfoMapper.deleteByIds(ids);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("删除失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }
     @Override
     public JsonResult batchImport(String filePath) {
         JsonResult jr = new JsonResult();
         ImportResult result = new ImportResult();

         String rootDir = PropertiesEnum.getPropertyValue(PropertiesEnum.FILE_UPLOAD_ROOT);
         String realPath = rootDir + "/" + filePath;

         Map data = new HashMap();
         List<Biz4sinfo> biz4sinfos = new ArrayList<>();
         data.put("biz4sinfos", biz4sinfos);
         try (InputStream excel = new FileInputStream(realPath)) {
             ExcelUtil.readByTemplate("/template/biz/biz4sinfo.xml", data, excel);
             //sysPermissionMapper.deleteAll();
             for (Biz4sinfo biz4sinfo : biz4sinfos){
                 biz4sinfo.setDataStatus(CommonConstant.YES);
                 this.save(biz4sinfo);
             }
             result.setTotalNum(biz4sinfos.size());
             jr.setResult(ResultCode.SUCCESS);
             jr.setData(result);
         }catch (Exception ex){
             log.error("读取文件失败", ex);
             jr.setResult(ResultCode.ERROR);
         }
         return jr;
     }
}